package com.wb.config;

import com.google.common.collect.Maps;
import com.wb.bean.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.Map;

public class JWTTokenEnhancer implements TokenEnhancer {

    private final Logger logger = LoggerFactory.getLogger(JWTTokenEnhancer.class);

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        logger.info("/-  oAuth2AccessToken : {} , oAuth2Authentication : {}" , oAuth2AccessToken , oAuth2Authentication);
        try {
            User user = (User)oAuth2Authentication.getPrincipal();
            Map<String,Object> maps = Maps.newHashMap();
            maps.put("user" , user);
            ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(maps);
        } catch (Exception e) {
            logger.error("" , e);
        }
        return oAuth2AccessToken;
    }
}
